package com.bac.rentmap.configuration;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.core.io.Resource;

public abstract class AbstractFileConfigurationManager<T> implements
		ConfigurationManager<T> {
	private Resource resource;
	private Properties properties;

	public T getPropertyValue(String propertyName) throws IOException {
		mayBeLoadProperties();
		String rawPropertyValue = properties.getProperty(propertyName);
		if (rawPropertyValue == null)
			return null;
		return convert2PropertyValue(rawPropertyValue);
	}

	private void mayBeLoadProperties() throws IOException {
		if (properties == null) {
			properties = new Properties();
			InputStream inputStream = resource.getInputStream();
			properties.load(inputStream);
			inputStream.close();
		}
	}

	protected abstract T convert2PropertyValue(String rawPropertyValue);

	@Required
	public void setResource(Resource resource) {
		this.resource = resource;
	}
}
